/*
 * Created on 04-sep-2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package core.loaders;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.StringTokenizer;

import sun.awt.image.ImageFormatException;
import utils.DBG;
import core.ImageLibrary;
import core.loaders.io.GameFileReader;

public class ResourceLoader {
	String filename;

	GameFileReader reader;

	ImageLibrary ilib;

	public ResourceLoader(String filename, ImageLibrary ilib) {
		this.filename = filename;
		reader = new GameFileReader(filename);
		this.ilib = ilib;
	}

	public void loadImages() {
		try{
		reader.openFile();
		}catch(Exception e){
			DBG.showMsg(e.toString(), this);
		}
		String linea = reader.readLine();
		
		while (linea != null) {
			if (linea.startsWith("/gui/res")) {
				System.out.println(linea);
				StringTokenizer st = new StringTokenizer(linea, " ");
				String v1 = st.nextToken();
				String v2 = st.nextToken();
				ilib.load(v1, v2);
			}
			linea = reader.readLine();
			DBG.showMsg(linea, this);
		}
		reader.closeFile();
	}

	/**
	 * Retorna una BufferedImage.
	 * 
	 * @param filename
	 * @return
	 * @throws IOException
	 * @throws ImageFormatException
	 */
	public BufferedImage loadBufferedImage(String filename) throws IOException,
			ImageFormatException {
		BufferedImage bi = javax.imageio.ImageIO
				.read(new java.io.File(filename));
		return bi;
	}

}
